using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Ui : MonoBehaviour, ISavable
{
    public UiFadeScreen FadeScreen { get; private set; }
    public TextMeshProUGUI EndText { get; private set; }
    private Button restartGameButton;
    public UiItemTooltip ItemTooltip { get; private set; }
    public UiStatTooltip StatTooltip { get; private set; }
    public UiSkillTooltip SkillTooltip { get; private set; }
    public Transform CharacterUi { get; private set; }
    public Transform SkillTreeUi { get; private set; }
    public Transform CraftUi { get; private set; }
    public Transform OptionsUi { get; private set; }
    public Transform InventoryUi { get; private set; }
    public Transform StashUi { get; private set; }
    public Transform EquipmentUi { get; private set; }
    public Transform StatUi { get; private set; }
    public UiCraftWindow UiCraftWindow { get; private set; }
    public Transform CraftSlotsParent { get; private set; }
    public GameObject InGameUi { get; private set; }
    private UiVolumeSlider[] volumeSliders;

    private void Awake()
    {
        FadeScreen = GetComponentInChildren<UiFadeScreen>();
        EndText = transform.Find("EndText").GetComponent<TextMeshProUGUI>();
        restartGameButton = transform.Find("RestartGameButton").GetComponent<Button>();
        ItemTooltip = GetComponentInChildren<UiItemTooltip>();
        StatTooltip = GetComponentInChildren<UiStatTooltip>();
        UiCraftWindow = GetComponentInChildren<UiCraftWindow>();
        SkillTooltip = GetComponentInChildren<UiSkillTooltip>();
        InGameUi = GetComponentInChildren<UiInGame>().gameObject;
        CharacterUi = transform.Find("CharacterUi").transform;
        SkillTreeUi = transform.Find("SkillTreeUi").transform;
        CraftUi = transform.Find("CraftUi").transform;
        OptionsUi = transform.Find("OptionsUi").transform;
        InventoryUi = GameObject.Find("InventoryUi").transform;
        StashUi = GameObject.Find("StashUi").transform;
        EquipmentUi = GameObject.Find("EquipmentUi").transform;
        StatUi = GameObject.Find("StatsUi").transform;
        CraftSlotsParent = GameObject.Find("CraftSlotsParent").transform;
        volumeSliders = GetComponentsInChildren<UiVolumeSlider>();
    }

    private void Start()
    {
        restartGameButton.onClick.AddListener(() => GameManager.Instance.RestartScene());
        // restartGameButton.gameObject.SetActive(false);
        EndText.color = Color.red;
        EndText.gameObject.SetActive(false);
        ItemTooltip.Hide();
        StatTooltip.Hide();
        SkillTooltip.Hide();
        SwitchTo(InGameUi);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            SwitchWithKeyTo(CharacterUi.gameObject);
        }
        if (Input.GetKeyDown(KeyCode.T))
        {
            SwitchWithKeyTo(SkillTreeUi.gameObject);
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            SwitchWithKeyTo(CraftUi.gameObject);
        }
        if (Input.GetKeyDown(KeyCode.V))
        {
            SwitchWithKeyTo(OptionsUi.gameObject);
        }
    }

    public void SwitchTo(GameObject menu)
    {
        foreach (Transform child in transform)
        {
            if (child.TryGetComponent(out UiFadeScreen _)) continue;
            child.gameObject.SetActive(false);
        }
        AudioManager.Instance.PlaySfx(7, null);
        menu?.SetActive(true);
        GameManager.Instance.Pause(menu != InGameUi);
    }

    public void SwitchWithKeyTo(GameObject menu)
    {
        if (menu != null && menu.activeSelf)
        {
            menu.SetActive(false);
            CheckForInGameUi();
            return;
        }
        SwitchTo(menu);
    }

    private void CheckForInGameUi()
    {
        if (transform.Cast<Transform>().Any(child => child.gameObject.activeSelf))
        {
            if (Time.timeScale == 0f)
            {
                GameManager.Instance.Pause(false);
            }
            return;
        }
        SwitchTo(InGameUi);
    }

    public void ShowFadeScreen()
    {
        FadeScreen.FadeOut();
        _ = StartCoroutine(EndScreenCoroutine());
    }

    private IEnumerator EndScreenCoroutine()
    {
        float seconds = FadeScreen.Anim.GetCurrentAnimatorClipInfo(0).Length;
        yield return new WaitForSeconds(seconds);
        // todo: 用代码模拟曲线动画控制alpha
        EndText.gameObject.SetActive(true);
        yield return new WaitForSeconds(seconds);
        restartGameButton.gameObject.SetActive(true);
    }

    public void Load(GameData data)
    {
        foreach (KeyValuePair<string, float> pair in data.volumeSettings)
        {
            foreach (UiVolumeSlider volume in volumeSliders)
            {
                if (volume.parameter == pair.Key)
                {
                    volume.Load(pair.Value);
                    // volume.SliderValue(pair.Value); // 设置音频混音器的参数
                }
            }
        }
    }

    public void Save(ref GameData data)
    {
        data.volumeSettings.Clear();
        foreach (var volume in volumeSliders)
        {
            data.volumeSettings.Add(volume.parameter, volume.Slider.value);
        }
    }
}